﻿// Open Source: MIT License (http://www.opensource.org/licenses/mit-license.php)
// Project: YoProgramo.NET (http://code.google.com/p/yoprogramo/)
// Autor: Fabian Figueredo (info@fabianfigueredo.com.ar)
// Blog: blog.yoprogramo.net

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Web.Mvc;
using Castle.Core;
using Castle.MicroKernel.Registration;
using Castle.Windsor;
using Castle.Windsor.Configuration.Interpreters;
using YoProgramo.Domain.Core.Exceptions;

namespace YoProgramo.Infrastructure.Tools.Windsor
{
    public class IoChelper {
        public WindsorContainer windsorContainer;

        public IoChelper()
        {            
        }

        public IoChelper(WindsorContainer c) {
            windsorContainer = c;
        }

        public WindsorContainer Initialize()
        {
            windsorContainer = new WindsorContainer(new XmlInterpreter());
            return windsorContainer;
        }

        public void RegisterControllers(Assembly assembly)
        {
            try
            {
                List<Type> controllersTypes = assembly.GetExportedTypes()                    
                    .Where(
                    t => t.IsClass == true
                         && t.IsAbstract == false
                         && t.Namespace.Contains("Controllers")
                         && t.BaseType == typeof(Controller)
                    ).ToList();

                foreach (Type type in controllersTypes) {
                    windsorContainer.AddComponentLifeStyle(type.Name, type.GetInterfaces()[0], type, LifestyleType.Transient);
                }

            }
            catch(Exception ex)
            {
                throw new YPBaseException("Error al registrar controladores en IoC", ex);
            }
        }
       
        public void AddRepositoriesTo(Assembly assembly) {
            windsorContainer.Register(AllTypes.Pick().FromAssembly(assembly).Configure(c => c.LifeStyle.Transient)
                                          .If(s => s.Namespace.Contains("Repositories")) 
                                          .WithService.FirstInterface());

        }

        public void AddServiciesTo(Assembly assembly) {
            windsorContainer.Register(AllTypes.Pick().FromAssembly(assembly).Configure(c => c.LifeStyle.Transient)
                                          .If(w => w.Namespace.Contains("Service"))
                                          .WithService.FirstInterface());
        }

        public void AddCoreTypesTo(Assembly coreAssembly) {
            windsorContainer.Register(AllTypes.Pick().FromAssembly(coreAssembly).Configure(c => c.LifeStyle.Transient)
                                          .If(w => w.Namespace.Contains("Core.Entities"))
                                          .WithService.FirstInterface());
        }

    }
}